package homework;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

public class HomeWorkDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        //获取job实例对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "HomeWorkDriver");

        job.setJarByClass(HomeWorkDriver.class);
        //关联mapper、reducer类
        job.setMapperClass(HomeWorkMapper.class);
        job.setReducerClass(HomeWorkReducer.class);

        //设置mapper和reducer输出类型
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(NullWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //预聚合小文件
        job.setInputFormatClass(CombineTextInputFormat.class);
        CombineTextInputFormat.setMaxInputSplitSize(job, 1024*1024*4);
        //自定义分区
        job.setPartitionerClass(HomeWorkPartitioner.class);

        //删输出文件
        File output = new File("e:/tempFile/data/output");
        if (output.exists()) {
            FileUtils.deleteDirectory(output);
        }

        //指定输入输出数据文件
        FileInputFormat.setInputPaths(job, new Path("e:/tempFile/data/input"));
        FileOutputFormat.setOutputPath(job, new Path(output.getPath()));

        //提交yarn
        final boolean flag = job.waitForCompletion(true);
        System.exit(flag ? 0 : 1);
    }
}
